using System;
using System.IO;
using System.Web.UI;
namespace JQueryControlToolkit.Controls
{
	[ParseChildren(true), PersistChildren(false)]
	public class Button : Control, JSObject, IPostBackEventHandler
	{
		public event EventHandler ServerClick;
		public string Name
		{
			get;
			set;
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public string OnClick
		{
			get;
			set;
		}
		public bool RequiresValidation
		{
			get;
			set;
		}
		public string ValidationGroup
		{
			get;
			set;
		}
		public string ToScript()
		{
			return Extensions.ToScript(this);
		}
		public void ToScript(TextWriter toScript)
		{
			toScript.Write(DomResult.Escape(this.Name));
			toScript.Write(" : ");
			string onClick = (this.OnClick == null) ? string.Empty : this.OnClick.Trim();
			int indexOfBody = onClick.LastIndexOf('}');
			if (this.ServerClick == null)
			{
				if (onClick.StartsWith("function(") || onClick.StartsWith("function "))
				{
					toScript.Write(onClick.Substring(0, indexOfBody));
					if (this.RequiresValidation)
					{
						toScript.Write("return ");
						toScript.Write(this.Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this, null, null, false, false, false, false, this.RequiresValidation, this.ValidationGroup)));
						toScript.WriteLine(";");
					}
					toScript.Write(onClick.Substring(indexOfBody));
				}
				else
				{
					toScript.Write("function() {");
					if (!string.IsNullOrEmpty(onClick))
					{
						toScript.Write(onClick);
						toScript.Write("(arguments);");
					}
					if (this.RequiresValidation)
					{
						toScript.Write("return ");
						toScript.Write(this.Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this, null, null, false, false, false, false, this.RequiresValidation, this.ValidationGroup)));
						toScript.WriteLine(";");
					}
					toScript.WriteLine("}");
				}
			}
			else
			{
				if (onClick.StartsWith("function(") || onClick.StartsWith("function "))
				{
					toScript.Write(onClick.Substring(0, indexOfBody));
					toScript.Write("return ");
					toScript.Write(this.Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this, null, null, false, false, false, true, this.RequiresValidation, this.ValidationGroup)));
					toScript.Write(";");
					toScript.Write(onClick.Substring(indexOfBody));
				}
				else
				{
					toScript.Write("function(){");
					if (!string.IsNullOrEmpty(onClick))
					{
						toScript.Write(onClick);
						toScript.Write("(arguments);");
					}
					toScript.Write("return ");
					toScript.Write(this.Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this, null, null, false, false, false, true, this.RequiresValidation, this.ValidationGroup)));
					toScript.Write(";");
					toScript.Write("}");
				}
			}
		}
		public void RaisePostBackEvent(string eventArgument)
		{
			if (this.ServerClick != null)
			{
				this.ServerClick(this, EventArgs.Empty);
			}
		}
	}
}
